package com.imooc.common.util;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.Map;

@JsonInclude(Include.NON_NULL)
public class R<T> {
	
	public Integer code;
	public String msg;
	public T data;
	public Map map;
	
	
	public R() {
		super();
	}
	
	

	public R(Map map) {
		super();
		this.map = map;
	}



	public R(int code, String msg, T data, Map map) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.map = map;
	}
	
	

	public R(Integer code) {
		super();
		this.code = code;
	}
	
	

	public R(Code code, String msg) {
		super();
		this.code = code.getValue();
		this.msg = msg;
	}

	public R(T data) {
		super();
		this.code = SysCode.SUCCESS.getValue();
		this.msg = SysCode.SUCCESS.getInfo();
		this.data = data;
	}


	public R(String msg) {
		super();
		this.code = SysCode.SUCCESS.getValue();
		this.msg = msg;
	}

	/**
	 * 方法
	 * @param data
	 * @param msg
	 * @return
	 */



	public R<T> success(T data, String msg) {
		this.code = SysCode.SUCCESS.getValue();
		this.msg = msg;
		this.data = data;
		return this;
	}
	
	public R<T> success(T data) {
		this.code = SysCode.SUCCESS.getValue();
		this.msg = SysCode.SUCCESS.getInfo();
		this.data = data;
		return this;
	}

	public R<T> success() {
		this.code = SysCode.SUCCESS.getValue();
		this.msg = SysCode.SUCCESS.getInfo();
		return this;
	}

	public R<T> succMsg(String msg) {
		this.code = SysCode.SUCCESS.getValue();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public R<T> dataError() {
		this.code = SysCode.DATAERROR.getValue();
		this.msg = SysCode.DATAERROR.getInfo();
		return this;
	}

	public R<T> dataError(String msg) {
		this.code = SysCode.DATAERROR.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public R error(Throwable e) {
		this.msg = e.getMessage();
		this.code = SysCode.FAIL.getValue();
		return this;
	}

	public R<T> failMsg(String msg) {
		this.code = SysCode.FAIL.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public R<T> fail(String msg) {
		this.code = SysCode.FAIL.getValue();
		this.msg = msg;
		return this;
	}

	public R<T> fail() {
		this.code = SysCode.FAIL.getValue();
		this.msg = SysCode.FAIL.getInfo();
		return this;
	}

	public R<T> unauthorized() {
		this.code = SysCode.UNAUTHORIZED.ordinal();
		this.msg = SysCode.UNAUTHORIZED.getInfo();
		return this;
	}

	public R<T> unauthorized(String msg) {
		this.code = SysCode.UNAUTHORIZED.ordinal();
		this.msg = msg;
		return this;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	
	public String toStr() {
		return "{\"code\":" + code + ",\"msg\":\"" + msg + "\"}";
	}

}
